package sdk

import (
	"bytes"
	"crypto/md5"
	"encoding/hex"
	"strings"
)

// 获取签名
func GetSign(request []byte, key []byte) (sign string, err error) {
	var buffer bytes.Buffer
	buffer.Write(key)
	buffer.Write(request)
	buffer.Write(key)
	sign, err = GetMd5String(buffer.Bytes(), true)
	if err != nil {
		panic(err)
	}
	return sign, nil
	//return "sign value", nil
}

// 获取 MD5 加密字符串
func GetMd5String(p []byte, withUpper bool) (result string, err error) {
	h := md5.New()
	_, err = h.Write(p)
	if err != nil {
		panic(err)
	}
	result = hex.EncodeToString(h.Sum(nil))
	if withUpper {
		result = strings.ToUpper(result)
	}
	return result, nil
}
